function logs(s) {
	console.log(s);
}
function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
		if(what == where[i]){
			a=true;
			break;
		}
	}
	return a;
}
function thmonth(month) {
	var months = new Array();
		months['01'] = 'มกราคม'; months['02'] = 'กุมภาพันธ์'; months['03'] = 'มีนาคม';
		months['04'] = 'เมษายน'; months['05'] = 'พฤษภาคม'; months['06'] = 'มิถุนายน';
		months['07'] = 'กรกฎาคม'; months['08'] = 'สิงหาคม'; months['09'] = 'กันยายน';
		months['10'] = 'ตุลาคม'; months['11'] = 'พฤศจิกายน'; months['12'] = 'ธันวาคม';
	return months[month];
}
function thdate(xdate) {
	// var days = new Array("อาทิตย์","จันทร์","อังคาร", "พุธ", "พฤหัสบดี", "ศุกร์", "เสาร์");
	var days = new Array();
		days[0] = "อาทิตย์";
		days[1] = "จันทร์";
		days[2] = "อังคาร";
		days[3] = "พุธ";
		days[4] = "พฤหัสบดี";
		days[5] = "ศุกร์";
		days[6] = "เสาร์";
	
	    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
	    var parts=xdate.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
		var x = new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
		return days[x.getDay()];
}
function is_int(value){
	if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
		return true;
	}else {
		return false;
	}
}
function getPercent(all, part) {
	var x = (all > 0) ? (100 / all) * part : 0;
	// return parseInt(Math.ceil(x * 100) / 100);
	// var intvalue = Math.floor( floatvalue );
	// var intvalue = Math.ceil( floatvalue ); 
	// return Math.ceil(x * 100) / 100;
	
	return Math.round( Math.ceil(x * 100) / 100 );
}
function showloading() {
	// better ping to server first before send the data	
	$('#loading').css({'visibility':'visible'});
	delay = $.timer(1000,function(){
	});
	// $(el).ajaxStart(function(){ $('#loading').css({'visibility':'visible'}); });
}
function hideloading() {
	$('#loading').css({'visibility':'hidden'});
}
$(document).ready(function() {
	$('#password_field').focus();
	$('#login_form').ajaxStart(function(){ 
		$('#password_field').blur();
		showloading();
	});
	function loginResponse(result) {
		var obj = eval('('+result+')');
		// if (obj.f=="1") {
		// 	$('#failed').hide()
		// 	$('#success').show();
		// 	var delay = {};
		// 	delay = $.timer(3000,function(){
		// 		window.top.location="http://"+obj.url+"";
		// 	});
		// 	
		// };
		if (obj.f=="0") {
			delay = $.timer(1000,function(){
				// $('#failed').fadeIn('fast').fadeOut(2000);
				$('#success').hide();
				$('#failed').show()
				$('#password_field').focus();
				hideloading();
				// $('#failed').hide();
			});
		};
	}
	function timeoutResponse() {
		var ou = encodeURI(window.top.location);
		var ku = ou.replace("http://192.168.10.254/?url=","");
		// alert(ou);
		// alert(ku);
		$('#failed').hide()
		$('#success').show();
		window.top.location="http://"+ku+"";
	}
	$('#login_form').ajaxForm( { 
		url: 'ServiceLogin.php', 
		success: loginResponse, 
		timeout: 3000,
		error: timeoutResponse,
		datatype: ($.browser.msie) ? "text" : "xml" 
	});
	
	$('#logout_btn').click(function() {
		window.top.location="/logout";
	});
	
});
